# -*- coding: utf-8 -*-
# coding:utf-8
__author__ = 'lancelrq'

import re
import urllib2
from bs4 import BeautifulSoup


class StanfordMossSpider(object):

    def __init__(self, result_url):
        self.__result_url = result_url
        pass

    def getResult(self):
        # 获取Moss查重结果信息
        rpd = self.__http(self.__result_url)
        if rpd is None:
            return None

        r1 = re.compile("<td><a([^>]*)>([^<]*)</a>", re.I)
        r2 = re.compile("<td align=right>(\d+)", re.I)
        rpd = r1.sub("<td><a\\1>\\2</a></td>", rpd)
        rpd = r2.sub("<td align=right>\\1</td></tr>", rpd)
        rpd = rpd.replace("<TR><TH>File 1<TH>File 2<TH>Lines Matched", "<TR><TH>File 1</TH><TH>File 2</TH><TH>Lines Matched</TH></TR>")

        p = BeautifulSoup(rpd)
        matches = p.findAll('tr')
        result_list = []
        r = re.compile(r'(.*)\((\d+)%\)')
        for match in matches:
            tagAs = match.findAll('a')
            if len(tagAs) == 0:
                continue
            href0 = tagAs[0]['href']
            text0 = tagAs[0].getText()
            rtext0 = r.search(text0)
            text1 = tagAs[1].getText()
            rtext1 = r.search(text1)
            result_list.append({
                "fileName0": rtext0.group(1),
                "ratio0": rtext0.group(2),
                "fileName1": rtext1.group(1),
                "ratio1": rtext1.group(2),
                "url": href0
            })
        return result_list

    def getMatches(self, result_list):
        r = re.compile(r'(.*)match(\d+).html')
        rimg = re.compile(r'tm_(\d+)_(\d+).gif')
        matches_list = {}
        for rel in result_list:
            match_data = matches_list.get(rel.get('url'), {})
            murl = r.sub("\\1match\\2-top.html", rel.get('url'))
            mpd = self.__http(murl)
            if mpd is None:
                continue
            p = BeautifulSoup(mpd)
            matches = p.findAll("a")
            for match in matches:
                name = match['name']
                target = match['target']
                text = match.getText()
                match_item = match_data.get("%s-%s" % (name, target), {})
                if match.find('img') is not None:
                    src = match.find('img')['src']
                    s = rimg.search(src)
                    match_item['ratio'] = s.group(2)
                else:
                    lines = text.split('-')
                    if len(lines) == 2:
                        match_item['lineStart'] = lines[0]
                        match_item['lineEnd'] = lines[1]
                match_data["%s-%s" % (name, target)] = match_item
            matches_list[rel.get('url')] = match_data
        return matches_list

    def __http(self, url):
        try:
            http = urllib2.urlopen(url)
            data = http.read()
            return data
        except Exception, ex:
            print ex
            return None
